﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAO;
using System.Collections;
namespace BUS
{
    public static class ChiTietCongViec_BUS
    {
        public static List<ChiTietCongViec_DTO> DanhSachCacCongViecThuocNganhNghe(string Mng)
        {
            try
            {
                return ChiTietCongViec_DAO.DanhSachCacCongViecThuocNganhNghe(Mng);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<ChiTietCongViec_DTO> DanhSachCacCongViecThuocNhaTuyenDung(string Mct)
        {
            try
            {
                return ChiTietCongViec_DAO.DanhSachCacCongViecThuocNhaTuyenDung(Mct);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<ChiTietCongViec_DTO> DanhSachCacCongViecThuocNoiLamViec(string Mnlv)
        {
            try
            {
                return ChiTietCongViec_DAO.DanhSachCacCongViecThuocNoiLamViec(Mnlv);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<ChiTietCongViec_DTO> ChiTietCongViec(string Mcv)
        {
            try
            {
                return ChiTietCongViec_DAO.ChiTietCongViec(Mcv);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static IEnumerable DanhSachNoiLamViec()
        {
            return ChiTietCongViec_DAO.DanhSachNoiLamViec();
        }

        public static void CapNhatSoLuotXem(int SoLuotXem, string Mcv)
        {
            ChiTietCongViec_DAO.CapNhatSoLuotXem(SoLuotXem, Mcv);
        }

        public static List<ChiTietCongViec_DTO> TimKiemNhanh(string TuKhoa, int Mng, int Mnlv, int Snd, int Mcb, int Snkn, int Mlhcv, int Lmin, int Lmax, int Co1)
        {
            try
            {
                int Co2 = 0;
                List<ChiTietCongViec_DTO> DSCongViec = new List<ChiTietCongViec_DTO>();
                List<ChiTietCongViec_DTO> list = ChiTietCongViec_DAO.TimKiemNhanh(Mng, Mnlv);

                foreach (var cv in list)
                {

                    string temp = cv.ChucDanh.ToLower();
                    Co2 = 0;
                    if (TuKhoa != "")
                    {
                        if (temp.Contains(TuKhoa.ToLower()))
                            Co2++;
                    }
                    else
                    {
                        Co2++;
                    }

                    if (Snd == 0)
                    {
                        Co2++;
                    }
                    else if (Snd != -1 && (DateTime.Now.DayOfYear - cv.NgayDang.DayOfYear) == Snd)
                    {
                        Co2++;
                    }


                    if (Mng != 0 && cv.MaNganhNghe == Mng)
                    {
                        Co2++;
                    }
                    if (Mng == 0)
                    {
                        Co2++;
                    }
                    if (Mnlv != 0 && cv.MaNoiLamViec == Mnlv)
                    {
                        Co2++;
                    }
                    if (Mnlv == 0)
                    {
                        Co2++;
                    }

                    if (Mcb != 0 && cv.MaCapBac == Mcb)
                    {
                        Co2++;
                    }

                    if (Snkn != -1 && cv.SoNamKN >= Snkn)
                    {
                        Co2++;
                    }

                    if (Mlhcv != 0 && cv.MaLoaiHinhCongViec == Mlhcv)
                    {
                        Co2++;
                    }

                    if (Lmax == -1 && Lmin != -1)
                    {
                        if (cv.Luong >= Lmin && cv.Luong != 0)
                            Co2++;
                    }

                    if (Lmax != -1 && Lmin == -1)
                    {
                        if (cv.Luong <= Lmax && cv.Luong != 0)
                            Co2++;
                    }

                    if (Lmax != -1 && Lmin != -1)
                    {
                        if (cv.Luong <= Lmax && cv.Luong >= Lmin && cv.Luong != 0)
                            Co2++;
                    }

                    if (Co2 == Co1)
                    {
                        int Co3 = 0;
                        for (int j = 0; j < DSCongViec.Count; j++)
                            if (DSCongViec[j].MaCongViec == cv.MaCongViec)
                            {
                                Co3 = 1;
                                break;
                            }
                        if (Co3 == 0)
                        {
                            DSCongViec.Add(cv);
                        }
                    }
                }
                //}
                return DSCongViec;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<ChiTietCongViec_DTO> TimKiemNhanhDanhSachCacCongViecThuocNhaTuyenDung(string Mct, int Mng, int Mnlv, int Snd, int Mcb, int Snkn, int Mlhcv, int Lmin, int Lmax, int Co1)
        {
            try
            {
                int Co2 = 0;
                List<ChiTietCongViec_DTO> DSCongViec = new List<ChiTietCongViec_DTO>();
                List<ChiTietCongViec_DTO> list = ChiTietCongViec_DAO.TimKiemNhanhDanhSachCacCongViecThuocNhaTuyenDung(Mct);
                if (Co1 == 0)
                {
                    LayDanhSach(DSCongViec, list);
                }
                else
                {
                    foreach (var cv in list)
                    {
                        Co2 = 0;
                        if (Snd == 0)
                        {
                            Co2++;
                        }
                        else if (Snd != -1 && (DateTime.Now.DayOfYear - cv.NgayDang.DayOfYear) == Snd)
                        {
                            Co2++;
                        }


                        if (Mng != -1 && cv.MaNganhNghe == Mng)
                        {
                            Co2++;
                        }

                        if (Mnlv != -1 && cv.MaNoiLamViec == Mnlv)
                        {
                            Co2++;
                        }

                        if (Mcb != 0 && cv.MaCapBac == Mcb)
                        {
                            Co2++;
                        }

                        if (Snkn != -1 && cv.SoNamKN >= Snkn)
                        {
                            Co2++;
                        }

                        if (Mlhcv != 0 && cv.MaLoaiHinhCongViec == Mlhcv)
                        {
                            Co2++;
                        }

                        if (Lmax == -1 && Lmin != -1)
                        {
                            if (cv.Luong >= Lmin && cv.Luong != 0)
                                Co2++;
                        }

                        if (Lmax != -1 && Lmin == -1)
                        {
                            if (cv.Luong <= Lmax && cv.Luong != 0)
                                Co2++;
                        }

                        if (Lmax != -1 && Lmin != -1)
                        {
                            if (cv.Luong <= Lmax && cv.Luong >= Lmin && cv.Luong != 0)
                                Co2++;
                        }

                        if (Co2 == Co1)
                        {
                            int Co3 = 0;
                            for (int j = 0; j < DSCongViec.Count; j++)
                                if (DSCongViec[j].MaCongViec == cv.MaCongViec)
                                {
                                    Co3 = 1;
                                    break;
                                }
                            if (Co3 == 0)
                            {
                                DSCongViec.Add(cv);
                            }
                        }
                    }
                }
                return DSCongViec;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<ChiTietCongViec_DTO> TimKiemNhanhTungNganhNghe(string TuKhoa, int LoaiTuKhoa, string Mng, int Mnlv, int Snd, int Mcb, string SoNamKinhNghiem, int Mlhcv, int Lmin, int Lmax, int Co2)
        {
            int Snkn = -1;
            if (SoNamKinhNghiem != null)
                Snkn = int.Parse(SoNamKinhNghiem);
            int Co1 = 0;
            List<ChiTietCongViec_DTO> list = new List<ChiTietCongViec_DTO>();
            List<ChiTietCongViec_DTO> DSCongViec = new List<ChiTietCongViec_DTO>();
            list = ChiTietCongViec_DAO.TimKiemNhanh(int.Parse(Mng), Mnlv);
            foreach (var cv in list)
            {
                Co1 = 0;
                if (TuKhoa != "")
                {
                    if (LoaiTuKhoa == 0)
                    {
                        string temp = cv.ChucDanh.ToLower();
                        string temp1 = cv.MoTaCongViec.ToLower();
                        string temp2 = cv.YeuCauCongViec.ToLower();
                        string[] TuKhoaCon = TuKhoa.Split(' ');
                        for (int j = 0; j < TuKhoaCon.Length; j++)
                        {
                            if (temp.Contains(TuKhoaCon[j].ToLower()) || temp1.Contains(TuKhoaCon[j].ToLower()) || temp2.Contains(TuKhoaCon[j].ToLower()))
                            {
                                Co1++;
                                break;
                            }
                        }
                    }
                    if (LoaiTuKhoa == 1)
                    {
                        string temp = cv.ChucDanh.ToLower();
                        string temp1 = cv.MoTaCongViec.ToLower();
                        string temp2 = cv.YeuCauCongViec.ToLower();
                        if (temp.Contains(TuKhoa.ToLower()) || temp1.Contains(TuKhoa.ToLower()) || temp2.Contains(TuKhoa.ToLower()))
                        {
                            Co1++;
                        }
                    }
                    if (LoaiTuKhoa == 2)
                    {
                        string temp = cv.ChucDanh.ToLower();
                        if (temp.Contains(TuKhoa.ToLower()))
                            Co1++;
                    }
                }
                else
                {
                    Co1++;
                }

                if (Snd == 0)
                {
                    Co1++;
                }
                else if (Snd > 0 && (DateTime.Now.DayOfYear - cv.NgayDang.DayOfYear) == Snd)
                {
                    Co1++;
                }


                if (cv.MaNganhNghe == int.Parse(Mng))
                {
                    Co1++;
                }

                if (Mnlv != 0 && cv.MaNoiLamViec == Mnlv)
                {
                    Co1++;
                }
                if (Mnlv == 0)
                {
                    Co1++;
                }

                if (cv.MaCapBac == Mcb)
                {
                    Co1++;
                }
                if (Mcb == 0)
                {
                    Co1++;
                }

                if (cv.SoNamKN >= Snkn && Snkn != -1)
                {
                    Co1++;
                }

                if (cv.MaLoaiHinhCongViec == Mlhcv)
                {
                    Co1++;
                }
                if (Mlhcv == 0)
                {
                    Co1++;
                }

                if (Lmax == -1 && Lmin != -1)
                {
                    if (cv.Luong >= Lmin && cv.Luong != 0)
                        Co1++;
                }

                if (Lmax != -1 && Lmin == -1)
                {
                    if (cv.Luong <= Lmax && cv.Luong != 0)
                        Co1++;
                }

                if (Lmax != -1 && Lmin != -1)
                {
                    if (cv.Luong <= Lmax && cv.Luong >= Lmin && cv.Luong != 0)
                        Co1++;
                }

                if ((Lmax == 0 && Lmin == 0) || (Lmax == -1 && Lmin == -1))
                {
                    Co1++;
                }

                if (Co1 == Co2)
                {
                    DSCongViec.Add(cv);
                }
            }
            return DSCongViec;
        }

        public static List<ChiTietCongViec_DTO> TimKiemNangCao(string TuKhoa, int LoaiTuKhoa, string DsMng, string Mng, int Mnlv, int Snd, int Mcb, string SoNamKinhNghiem, int Mlhcv, int Lmin, int Lmax, int Co2)
        {
            try
            {
                List<ChiTietCongViec_DTO> DSCongViec = new List<ChiTietCongViec_DTO>();
                List<ChiTietCongViec_DTO> list = new List<ChiTietCongViec_DTO>();

                if (Mng == null)
                {
                    string[] ListMng = DsMng.Split(',');

                    for (int i = 0; i < ListMng.Length; i++)
                    {
                        list = ChiTietCongViec_BUS.TimKiemNhanhTungNganhNghe(TuKhoa, LoaiTuKhoa, ListMng[i], Mnlv, Snd, Mcb, SoNamKinhNghiem, Mlhcv, Lmin, Lmax, Co2);
                        int Co3 = 0;
                        for (int m = 0; m < list.Count; m++)
                        {
                            Co3 = 0;
                            for (int n = 0; n < DSCongViec.Count; n++)
                            {
                                if (DSCongViec[n].MaCongViec == list[m].MaCongViec)
                                {
                                    Co3 = 1;
                                    break;
                                }
                            }
                            if (Co3 == 0)
                            {
                                DSCongViec.Add(list[m]);
                            }
                        }
                    }
                }
                else
                {

                    list = ChiTietCongViec_BUS.TimKiemNhanhTungNganhNghe(TuKhoa, LoaiTuKhoa, Mng, Mnlv, Snd, Mcb, SoNamKinhNghiem, Mlhcv, Lmin, Lmax, Co2);
                    int Co3 = 0;
                    for (int m = 0; m < list.Count; m++)
                    {
                        Co3 = 0;
                        for (int n = 0; n < DSCongViec.Count; n++)
                        {
                            if (DSCongViec[n].MaCongViec == list[m].MaCongViec)
                            {
                                Co3 = 1;
                                break;
                            }
                        }
                        if (Co3 == 0)
                        {
                            DSCongViec.Add(list[m]);
                        }
                    }
                    //DSCongViec = TimKiemNhanh(TuKhoa, int.Parse(Mng), Mnlv, Snd, Mcb, Snkn, Mlhcv, Lmin, Lmax, Co2);
                }
                return DSCongViec;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private static void LayDanhSach(List<ChiTietCongViec_DTO> DSCongViec, List<ChiTietCongViec_DTO> list)
        {
            DSCongViec.Add(list[0]);
            for (int i = 0; i < list.Count; i++)
            {
                int Co3 = 0;
                for (int j = 0; j < DSCongViec.Count; j++)
                    if (DSCongViec[j].MaCongViec == list[i].MaCongViec)
                    {
                        Co3 = 1;
                        break;
                    }
                if (Co3 == 0)
                {
                    DSCongViec.Add(list[i]);
                }
            }
        }

        public static List<ChiTietCongViec_DTO> TimKiemNhanhTheoNamKinhNghiem(int Mng, int Mnlv, string TuKhoa, int Snkn)
        {
            List<ChiTietCongViec_DTO> DSCongViec = new List<ChiTietCongViec_DTO>();
            List<ChiTietCongViec_DTO> list = ChiTietCongViec_DAO.TimKiemNhanh(Mng, Mnlv);
            foreach (var cv in list)
            {
                ChiTietCongViec_DTO congviec = new ChiTietCongViec_DTO();
                string temp = cv.ChucDanh.ToLower();
                if (temp.Contains(TuKhoa.ToLower()) && cv.SoNamKN == Snkn)
                {
                    congviec = cv;
                    DSCongViec.Add(congviec);
                }
            }
            return DSCongViec;
        }

        public static List<ChiTietCongViec_DTO> TimKiemTheoNamKinhNghiemCuaNhaTuyenDung(string Mct, int Snkn)
        {
            List<ChiTietCongViec_DTO> DSCongViec = new List<ChiTietCongViec_DTO>();
            List<ChiTietCongViec_DTO> list = ChiTietCongViec_DAO.DanhSachCacCongViecThuocNhaTuyenDung(Mct);
            foreach (var cv in list)
            {
                ChiTietCongViec_DTO congviec = new ChiTietCongViec_DTO();
                if (cv.SoNamKN == Snkn)
                {
                    congviec = cv;
                    DSCongViec.Add(congviec);
                }
            }
            return DSCongViec;
        }

        public static IEnumerable CongViecCapGap()
        {
            return ChiTietCongViec_DAO.CongViecCapGap();
        }

        public static IEnumerable CongViecTheoLuong()
        {
            return ChiTietCongViec_DAO.CongViecTheoLuong();
        }

        public static IEnumerable CongViecTheoNgayDang()
        {
            return ChiTietCongViec_DAO.CongViecTheoNgayDang();
        }

        public static IEnumerable DanhSachCacLoaiHinhCongViec()
        {
            return ChiTietCongViec_DAO.DanhSachCacLoaiHinhCongViec();
        }

        public static IEnumerable DanhSachCacCapBac()
        {
            return ChiTietCongViec_DAO.DanhSachCacCapBac();
        }

        public static void HuyCongViec(string Mcv)
        {
            ChiTietCongViec_DAO.HuyCongViec(Mcv);
        }

        public static List<ChiTietCongViec_DTO> LayCongViecDaLuu(int UserID)
        {
            return ChiTietCongViec_DAO.LayCongViecDaLuu(UserID);
        }

        public static void LayMaCongTyTuMaCongViec(string Mcv, ref string Mct, ref string TrangThai)
        {
            ChiTietCongViec_DAO.LayMaCongTyTuMaCongViec(Mcv, ref Mct, ref TrangThai);
        }

        public static IEnumerable DanhSachCacTinSaiPham()
        {
            return ChiTietCongViec_DAO.DanhSachCacTinSaiPham();
        }

        public static void ChoPhepDangLaiTin(string Mcv)
        {
            ChiTietCongViec_DAO.ChoPhepDangLaiTin(Mcv);
        }

        public static List<ChiTietCongViec_DTO> ChiTietCongViecSaiPham(string Mcv)
        {
            return ChiTietCongViec_DAO.ChiTietCongViecSaiPham(Mcv);
        }
    }
}
